package app_dao

import (
	"errors"
	"fmt"
	"hft/Database/admin"
	"hft/Http/Services/front/app/app_model"
	"hft/e"

	"gorm.io/gorm"
)

type AppBase struct{}

// 基础
func (*AppBase) GetRegion(db *gorm.DB, city int) (string, error) {
	var region *admin.Region
	if err := db.First(&region, city).Error; err != nil {
		return "", errors.New(e.RegionNotFound)
	}
	return fmt.Sprintf("%v%v,", region.Link, region.ID), nil
}

func (*AppBase) GetTestLou(db *gorm.DB, teSeName string, regionLink string) (list []*app_model.Name) {
	var louList []*admin.LouPan
	if teSeName != "" {
		db.Joins("LEFT JOIN hft_loupan_tese ON `hft_loupans`.`id` = `hft_loupan_tese`.`lou_pan_id`").Where("region_link LIKE ?", fmt.Sprintf("%v%%", regionLink)).Where(fmt.Sprintf("`hft_loupan_tese`.`lou_pan_te_se_name` = \"%v\"", teSeName)).Order("sort desc").Limit(45).Find(&louList)
	} else {
		db.Where("region_link LIKE ?", fmt.Sprintf("%v%%", regionLink)).Order("sort desc").Limit(45).Find(&louList)
	}

	var newLouList = make([]*app_model.Name, 0)
	for _, v := range louList {
		newLouList = append(newLouList, &app_model.Name{ID: v.ID, Name: v.Name})
	}
	return newLouList
}

// 获取区域
func (*AppBase) GetCityRegion(db *gorm.DB, city int) (list []*app_model.Name) {
	var regions []*admin.Region
	db.Where("p_id = ?", city).Order("sort desc").Limit(45).Find(&regions)
	var regionList = make([]*app_model.Name, 0)
	for _, v := range regions {
		regionList = append(regionList, &app_model.Name{ID: v.ID, Name: v.Name})
	}
	return regionList
}

var AppBaseDao = new(AppBase)
